﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;
using DevExpress.XtraEditors;
using System.Diagnostics;

namespace GE_Management_Tool.Home_Page.UserControl
{
    public partial class uc1004_To_Do_List_Manager : DevExpress.XtraEditors.XtraUserControl
    {
        CAppointments m_objSelectedAppointment;
        CAppointments m_objSelectedAppointment_Received;
        CAppointments m_objSelectedFeedback;
        int m_iCountTime = 30;
        IList<CAppointments> m_lsAppointment;
        IList<CAppointments> m_lsAppointment_Received;
        IList<CAppointments> m_lsFeedbacks;
        IList<CAsset> m_lsAsset;

        public uc1004_To_Do_List_Manager()
        {
            InitializeComponent();
            if (CSession.Active_Staff == null)
            {
                throw new Exception("Network Error !");
            }
            CCommon_Function.Format_All(this);
            this.grvData.OptionsBehavior.Editable = false;
            this.grvAsset.OptionsBehavior.Editable = true;
            this.grvFeedbacks.OptionsBehavior.Editable = true;
        }

        private void Init_Data()
        {
            m_lsAppointment = new List<CAppointments>();
            CAppointments_Controller v_objCtrlTasks = new CAppointments_Controller();
            m_lsAppointment = v_objCtrlTasks.A1004_sp_sel_List_Appointments_By_Staff_ID(CSession.Active_Staff.Auto_ID);
            grdData.DataSource = m_lsAppointment;

            m_lsAppointment_Received = new List<CAppointments>();
            if(CSession.Active_Staff.Regency_ID == CUtility.Convert_To_Int32(EStaff_Type.Art_Lead))
            {
                m_lsAppointment_Received = v_objCtrlTasks.A1004_sp_sel_List_Appointments_By_Status(CUtility.Convert_To_Int32(ETasks_Status.Wait_Art_Lead), false);
            }
            if(CSession.Active_Staff.Regency_ID == CUtility.Convert_To_Int32(EStaff_Type.Technician))
            {
                m_lsAppointment_Received = v_objCtrlTasks.A1004_sp_sel_List_Appointments_By_Status(CUtility.Convert_To_Int32(ETasks_Status.Wait_Technicial), false);
            }
            IList<CProject> v_lsProject = new List<CProject>();
            CProject_Controller v_CtrlProject = new CProject_Controller();
            v_lsProject = v_CtrlProject.List_Project();
            lkeProject.DataSource = v_lsProject;
            lkeProject_Asset.DataSource = v_lsProject;
            grdReceived.DataSource = m_lsAppointment_Received;

        }

        private void Load_Task_Asset(int piTask_ID)
        {
            m_lsAsset = new List<CAsset>();
            CAsset_Controller v_CtrlAsset = new CAsset_Controller();
            m_lsAsset = v_CtrlAsset.A1004_sp_sel_List_Tasks_By_Task_ID(piTask_ID);
        }

        private void uc1004_To_Do_List_Load(object sender, EventArgs e)
        {
            try
            {
                btnRefresh_Click(null, null);
                TimerRefreshData.Start();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void TimerRefreshData_Tick(object sender, EventArgs e)
        {
            try
            {
                chkStop_Auto_Refresh.Text = "Data Will Be Refresh in " + m_iCountTime.ToString("D2") + " s.";
                if (m_iCountTime == 0)
                {
                    Init_Data();
                    grdData.RefreshDataSource();
                    m_iCountTime = 30;
                }
                else
                {
                    m_iCountTime--;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grvData_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objSelectedAppointment = (CAppointments)grvData.GetRow(grvData.FocusedRowHandle);
                Load_Task_Asset(m_objSelectedAppointment.Auto_ID);
                grdAsset.DataSource = m_lsAsset;

                //set loại thông tin xử lý:
                if (m_objSelectedAppointment.Status == 0)
                {
                    btnStart.Enabled = true;
                    btnDone_And_Send_To_Technical.Enabled = false;
                }
                if (m_objSelectedAppointment.Status == 1)
                {
                    btnStart.Enabled = false;
                    btnDone_And_Send_To_Technical.Enabled = true;
                }
                if (m_objSelectedAppointment.Status == 2)
                {
                    btnStart.Enabled = false;
                    btnDone_And_Send_To_Technical.Enabled = false;
                }

                //load thông tin feedback của 1 task:
                m_lsFeedbacks = new List<CAppointments>();
                CAppointments_Controller v_CtrlAppointment = new CAppointments_Controller();
                m_lsFeedbacks = v_CtrlAppointment.A1004_sp_sel_List_Feedback_ID(m_objSelectedAppointment.Parent_ID, true);
                cAppointmentsBindingSource.DataSource = m_lsFeedbacks;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void chkStop_Auto_Refresh_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkStop_Auto_Refresh.Checked == true)
                {
                    TimerRefreshData.Stop();
                }
                else
                {
                    TimerRefreshData.Start();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                Init_Data();
                grdData.RefreshDataSource();
                m_iCountTime = 30;
                chkStop_Auto_Refresh.Text = "Data Will Be Refresh in " + m_iCountTime.ToString("D2") + " s.";
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                m_objSelectedAppointment.Status = 1;
                m_objSelectedAppointment.Actual_Start_Date = DateTime.Now;
                CAppointments_Controller v_CtrlApp = new CAppointments_Controller();
                v_CtrlApp.Update_Appointments(m_objSelectedAppointment);
                grdData.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDone_And_Send_To_Artlead_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDone_And_Send_To_Technical_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnRecieved_Click(object sender, EventArgs e)
        {
            try
            {
                if(chkStop_Auto_Refresh.Checked == true)
                {
                    chkStop_Auto_Refresh.Checked = false;
                }

                CAppointments v_objAppointment = (CAppointments)m_objSelectedAppointment_Received.Clone();
                v_objAppointment.Parent_ID = m_objSelectedAppointment_Received.Auto_ID;
                v_objAppointment.Subject = "Check " + v_objAppointment.Subject;
                v_objAppointment.Start_Date = DateTime.Now;
                v_objAppointment.End_Date = v_objAppointment.Start_Date.AddHours(8);
                v_objAppointment.Status = 0;
                v_objAppointment.Actual_Start_Date = CConst.DTM_VALUE_NULL;
                v_objAppointment.Actual_End_Date = CConst.DTM_VALUE_NULL;
                v_objAppointment.Resource_ID = CSession.Active_Staff.Auto_ID;

                CAppointments_Controller v_CtrlAppointment = new CAppointments_Controller();
                v_CtrlAppointment.Insert_Appointments(v_objAppointment);

                m_objSelectedAppointment_Received.Status = 2;
                v_CtrlAppointment.Update_Appointments(m_objSelectedAppointment_Received);

                chkStop_Auto_Refresh.Checked = true;

                XtraMessageBox.Show("Received Success !", "Warning", MessageBoxButtons.OK);

                btnRefresh_Click(null, null);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grvReceived_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objSelectedAppointment_Received = (CAppointments)grvReceived.GetRow(grvReceived.FocusedRowHandle);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            e.Handled = true;
            if (e.Button.ButtonType == NavigatorButtonType.Append)
            {
                //Thực hiện thao tác Add:
                CAppointments v_objApp = new CAppointments();
                CAppointments_Controller v_CtrlApp = new CAppointments_Controller();
                v_objApp = v_CtrlApp.Get_Appointments_By_ID(m_objSelectedAppointment.Parent_ID);

                frm1004_Feedbacks_Edit_Form v_frm = new frm1004_Feedbacks_Edit_Form();
                CAppointments v_obj = v_frm.Show_To_Add(v_objApp);
                if (v_obj != null)
                {
                    m_lsFeedbacks.Add(v_obj);
                }
                grdFeedbacks.RefreshDataSource();

            }
            if (e.Button.ButtonType == NavigatorButtonType.Edit)
            {
                //Thực hiện thao tác Edit:
                frm1004_Feedbacks_Edit_Form v_frm = new frm1004_Feedbacks_Edit_Form();
                v_frm.Show_To_Add(m_objSelectedFeedback);
            }
        }

        private void repositoryItemHyperLinkEdit1_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e)
        {
            Process.Start(@"c:\");
        }

        private void grvFeedbacks_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objSelectedFeedback = (CAppointments)grvFeedbacks.GetRow(grvFeedbacks.FocusedRowHandle);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        public void Close()
        {
            this.TimerRefreshData.Stop();
        }

        public void Start()
        {
            this.TimerRefreshData.Start();
        }

        private void btnEnd_Check_Click(object sender, EventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnStart_Check_Click(object sender, EventArgs e)
        {
            try
            {
                CAppointments v_objAppointment = (CAppointments)m_objSelectedAppointment_Received.Clone();
                v_objAppointment.Parent_ID = m_objSelectedAppointment_Received.Auto_ID;
                v_objAppointment.Subject = "Check " + v_objAppointment.Subject;
                v_objAppointment.Start_Date = DateTime.Now;
                v_objAppointment.End_Date = v_objAppointment.Start_Date.AddHours(8);
                v_objAppointment.Status = 0;
                v_objAppointment.Actual_Start_Date = CConst.DTM_VALUE_NULL;
                v_objAppointment.Actual_End_Date = CConst.DTM_VALUE_NULL;
                v_objAppointment.Resource_ID = CSession.Active_Staff.Auto_ID;

                XtraMessageBox.Show("Received Success !", "Warning", MessageBoxButtons.OK);

                btnRefresh_Click(null, null);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}
